<?php

namespace apiend\modules\user\models;

use Yii;
use apiend\models\User;
use common\models\user\UserConnect;
use common\helpers\Url;

/**
 * 解除第三方授权
 * ThirdPartyRelieveForm
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ThirdPartyRelieveForm extends \common\base\Model {

    /**
     * @var integer 用户UID
     */
    public $user_id;

    /**
     * @var string 第三方平台标识
     */
    public $client;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['client', 'user_id'], 'required'],
            [['client'], 'string'],
            ['client', 'in', 'range' => ['qq', 'weibo', 'wechat'], 'message' => '{attribute}不正确'],
        ];
    }

    /**
     * Signs user up.
     * @return User|null the saved model or null if saving fails
     */
    public function save() {
        if (!$this->validate()) {
            $this->dumpError('授权解除失败');
        }
        $bind = false;
        $client = $this->client;
        $_icon = $client;
        $model = UserConnect::findOne($this->attributes);
        if ($model !== null && !$model->delete()) {
            $bind = true;
            $_icon .= '_color';
        }
        $icon = Url::attach('/assets/icons/client/' . $_icon . '.png');
        return compact('client', 'bind', 'icon');
    }

}
